package com.iiiu.payment.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.iiiu.payment.domain.entity.Category;
import com.iiiu.payment.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.iiiu.payment.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 分类控制器
 */
@Tag(name = "分类管理", description = "分类相关API")
@RestController
@RequestMapping("/api/category")
@Validated
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Operation(summary = "根据用户ID查询分类列表", description = "查询指定用户的所有分类")
    @GetMapping("/user/{userId}")
    public List<Category> getCategoriesByUserId(
            @Parameter(description = "用户ID") @PathVariable @NotNull Long userId) {
        
        List<Category> categories = categoryService.getByUserId(userId);
        return categories;
    }

    @Operation(summary = "根据ID查询分类", description = "根据分类ID查询详细信息")
    @GetMapping("/{id}")
    public Category getCategoryById(
            @Parameter(description = "分类ID") @PathVariable @NotNull Long id) {
        
        Category category = categoryService.getById(id);
        
        if (category != null && category.getIsDelete() == 0) {
            return category;
        }

        return null;

    }

    @Operation(summary = "创建分类", description = "创建新的分类")
    @PostMapping
    public Category createCategory(@Valid @RequestBody Category category) {
        Category createdCategory = categoryService.createCategory(category);
        return createdCategory;
    }

    @Operation(summary = "更新分类", description = "更新分类信息")
    @PutMapping("/{id}")
    public Category updateCategory(
            @Parameter(description = "分类ID") @PathVariable @NotNull Long id,
            @Valid @RequestBody Category category) {
        category.setId(id);
        Category updatedCategory = categoryService.updateCategory(category);
        return updatedCategory;
    }

    @Operation(summary = "删除分类", description = "软删除分类")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCategory(
            @Parameter(description = "分类ID") @PathVariable @NotNull Long id,
            @Parameter(description = "用户ID") @RequestParam @NotNull Long userId) {
        boolean deleted = categoryService.deleteCategory(id, userId);
        return deleted ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
    }

    @Operation(summary = "检查分类名称是否存在", description = "检查指定用户下分类名称是否已存在")
    @GetMapping("/exists")
    public Map<String, Boolean> checkNameExists(
            @Parameter(description = "用户ID") @RequestParam @NotNull Long userId,
            @Parameter(description = "分类名称") @RequestParam String name) {
        boolean exists = categoryService.existsByUserIdAndName(userId, name);
        return Map.of("exists", exists);
    }

    @Operation(summary = "更新分类排序", description = "批量更新分类的排序顺序")
    @PutMapping("/sort")
    public Void updateSortOrder(
            @Parameter(description = "用户ID") @RequestParam @NotNull Long userId,
            @RequestBody List<Long> categoryIds) {
        boolean updated = categoryService.updateSortOrder(userId, categoryIds);
        return null;
    }
}